//
// interp1.cpp
//
// Code generation for function 'interp1'
//

// Include files
#include "interp1.h"
#include "detrendIBI_priors_data.h"
#include "eml_int_forloop_overflow_check.h"
#include "indexShapeCheck.h"
#include "ppval.h"
#include "rt_nonfinite.h"
#include "coder_array.h"
#include "mwmathutil.h"
#include "omp.h"

// Type Definitions
struct c_struct_T {
  coder::array<real_T, 2U> breaks;
  coder::array<real_T, 2U> coefs;
};

// Variable Definitions
static emlrtRSInfo db_emlrtRSI{
    49,        // lineNo
    "interp1", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo eb_emlrtRSI{
    162,            // lineNo
    "interp1_work", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo fb_emlrtRSI{
    192,            // lineNo
    "interp1_work", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo gb_emlrtRSI{
    200,            // lineNo
    "interp1_work", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo hb_emlrtRSI{
    204,            // lineNo
    "interp1_work", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo ib_emlrtRSI{
    265,            // lineNo
    "interp1_work", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo kb_emlrtRSI{
    331,                    // lineNo
    "interp1SplineOrPCHIP", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo lb_emlrtRSI{
    354,                    // lineNo
    "interp1SplineOrPCHIP", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo mb_emlrtRSI{
    358,                    // lineNo
    "interp1SplineOrPCHIP", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pathName
};

static emlrtRSInfo nb_emlrtRSI{
    22,       // lineNo
    "spline", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pathName
};

static emlrtRSInfo ob_emlrtRSI{
    35,         // lineNo
    "splinepp", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pathName
};

static emlrtRSInfo pb_emlrtRSI{
    41,         // lineNo
    "splinepp", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pathName
};

static emlrtRSInfo qb_emlrtRSI{
    69,         // lineNo
    "splinepp", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pathName
};

static emlrtRSInfo ub_emlrtRSI{
    164,        // lineNo
    "splinepp", // fcnName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pathName
};

static emlrtRTEInfo e_emlrtRTEI{
    137,            // lineNo
    23,             // colNo
    "interp1_work", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pName
};

static emlrtRTEInfo f_emlrtRTEI{
    153,            // lineNo
    15,             // colNo
    "interp1_work", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pName
};

static emlrtRTEInfo g_emlrtRTEI{
    164,            // lineNo
    13,             // colNo
    "interp1_work", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pName
};

static emlrtRTEInfo h_emlrtRTEI{
    206,            // lineNo
    13,             // colNo
    "interp1_work", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pName
};

static emlrtRTEInfo jb_emlrtRTEI{
    33,       // lineNo
    15,       // colNo
    "chckxy", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\private\\chckxy"
    ".m" // pName
};

static emlrtRTEInfo kb_emlrtRTEI{
    37,       // lineNo
    15,       // colNo
    "chckxy", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\private\\chckxy"
    ".m" // pName
};

static emlrtRTEInfo lb_emlrtRTEI{
    39,       // lineNo
    1,        // colNo
    "chckxy", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\private\\chckxy"
    ".m" // pName
};

static emlrtRTEInfo mb_emlrtRTEI{
    43,       // lineNo
    19,       // colNo
    "chckxy", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\private\\chckxy"
    ".m" // pName
};

static emlrtDCInfo e_emlrtDCI{
    63,         // lineNo
    32,         // colNo
    "splinepp", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m", // pName
    4 // checkKind
};

static emlrtDCInfo f_emlrtDCI{
    64,         // lineNo
    35,         // colNo
    "splinepp", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m", // pName
    4 // checkKind
};

static emlrtDCInfo g_emlrtDCI{
    32,         // lineNo
    37,         // colNo
    "pwchcore", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\private\\pwchco"
    "re.m", // pName
    4       // checkKind
};

static emlrtRTEInfo cc_emlrtRTEI{
    49,        // lineNo
    49,        // colNo
    "interp1", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pName
};

static emlrtRTEInfo dc_emlrtRTEI{
    50,        // lineNo
    25,        // colNo
    "interp1", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pName
};

static emlrtRTEInfo ec_emlrtRTEI{
    49,        // lineNo
    9,         // colNo
    "interp1", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\interp1.m" // pName
};

static emlrtRTEInfo kd_emlrtRTEI{
    63,       // lineNo
    24,       // colNo
    "spline", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pName
};

static emlrtRTEInfo ld_emlrtRTEI{
    64,       // lineNo
    27,       // colNo
    "spline", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pName
};

static emlrtRTEInfo md_emlrtRTEI{
    69,       // lineNo
    21,       // colNo
    "spline", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pName
};

static emlrtRTEInfo nd_emlrtRTEI{
    116,      // lineNo
    21,       // colNo
    "spline", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pName
};

static emlrtRTEInfo od_emlrtRTEI{
    32,         // lineNo
    31,         // colNo
    "pwchcore", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\private\\pwchco"
    "re.m" // pName
};

static emlrtRTEInfo pd_emlrtRTEI{
    22,       // lineNo
    5,        // colNo
    "spline", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pName
};

static emlrtRTEInfo qd_emlrtRTEI{
    192,      // lineNo
    24,       // colNo
    "spline", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pName
};

static emlrtRTEInfo rd_emlrtRTEI{
    41,       // lineNo
    10,       // colNo
    "spline", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pName
};

static emlrtRTEInfo sd_emlrtRTEI{
    220,      // lineNo
    9,        // colNo
    "spline", // fName
    "D:\\Program "
    "Files\\MATLAB\\R2022a\\toolbox\\eml\\lib\\matlab\\polyfun\\spline.m" // pName
};

// Function Declarations
namespace coder {
static void interp1SplineOrPCHIP(const emlrtStack *sp,
                                 const ::coder::array<real_T, 1U> &y,
                                 const ::coder::array<real_T, 1U> &xi,
                                 ::coder::array<real_T, 1U> &yi,
                                 const ::coder::array<real_T, 2U> &x);

static void interp1SplineOrPCHIP(const emlrtStack *sp,
                                 const ::coder::array<real_T, 1U> &y,
                                 const ::coder::array<real_T, 2U> &xi,
                                 ::coder::array<real_T, 2U> &yi,
                                 const ::coder::array<real_T, 1U> &x);

} // namespace coder

// Function Definitions
namespace coder {
static void interp1SplineOrPCHIP(const emlrtStack *sp,
                                 const ::coder::array<real_T, 1U> &y,
                                 const ::coder::array<real_T, 1U> &xi,
                                 ::coder::array<real_T, 1U> &yi,
                                 const ::coder::array<real_T, 2U> &x)
{
  array<real_T, 2U> dvdf;
  array<real_T, 2U> dx;
  array<real_T, 2U> md;
  array<real_T, 2U> s;
  c_struct_T pp;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack st;
  real_T pp1_coefs[4];
  real_T dnnm2;
  real_T dzzdx;
  real_T r;
  real_T szdvdf_idx_1;
  int32_T b_k;
  int32_T k;
  int32_T nxm1;
  int32_T szs_idx_1;
  int32_T yoffset;
  boolean_T emlrtHadParallelError{false};
  boolean_T exitg1;
  boolean_T has_endslopes;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b((emlrtCTX)sp);
  st.site = &kb_emlrtRSI;
  b_st.site = &nb_emlrtRSI;
  c_st.site = &ob_emlrtRSI;
  if (x.size(1) < 2) {
    emlrtErrorWithMessageIdR2018a(&c_st, &jb_emlrtRTEI,
                                  "MATLAB:chckxy:NotEnoughPts",
                                  "MATLAB:chckxy:NotEnoughPts", 0);
  }
  has_endslopes =
      ((!muDoubleScalarIsInf(x[0])) && (!muDoubleScalarIsNaN(x[0])));
  if (has_endslopes) {
    k = 0;
    exitg1 = false;
    while ((!exitg1) && (k <= x.size(1) - 2)) {
      if (!(x[k] < x[k + 1])) {
        has_endslopes = false;
        exitg1 = true;
      } else {
        k++;
      }
    }
  }
  if ((!has_endslopes) || muDoubleScalarIsInf(x[x.size(1) - 1])) {
    has_endslopes = false;
  }
  if (!has_endslopes) {
    emlrtErrorWithMessageIdR2018a(
        &c_st, &kb_emlrtRTEI, "Coder:toolbox:MustBeFiniteAndStrictlyIncreasing",
        "Coder:toolbox:MustBeFiniteAndStrictlyIncreasing", 0);
  }
  has_endslopes = false;
  k = 0;
  exitg1 = false;
  while ((!exitg1) && (k <= y.size(0) - 1)) {
    if (muDoubleScalarIsNaN(y[k])) {
      has_endslopes = true;
      exitg1 = true;
    } else {
      k++;
    }
  }
  if (has_endslopes) {
    emlrtErrorWithMessageIdR2018a(&c_st, &lb_emlrtRTEI,
                                  "Coder:toolbox:UnsupportedNaN",
                                  "Coder:toolbox:UnsupportedNaN", 0);
  }
  if ((y.size(0) != x.size(1)) && (y.size(0) != x.size(1) + 2)) {
    emlrtErrorWithMessageIdR2018a(&c_st, &mb_emlrtRTEI,
                                  "MATLAB:chckxy:NumSitesMismatchValues",
                                  "MATLAB:chckxy:NumSitesMismatchValues", 4, 12,
                                  x.size(1), 12, y.size(0));
  }
  has_endslopes = (y.size(0) == x.size(1) + 2);
  if ((x.size(1) <= 2) || ((x.size(1) <= 3) && (!has_endslopes))) {
    c_st.site = &pb_emlrtRSI;
    has_endslopes = (y.size(0) == x.size(1) + 2);
    if (x.size(1) <= 2) {
      if (has_endslopes) {
        yoffset = 4;
      } else {
        yoffset = 2;
      }
    } else {
      yoffset = 3;
    }
    pp.coefs.set_size(&qd_emlrtRTEI, &c_st, 1, yoffset);
    if (x.size(1) <= 2) {
      if (has_endslopes) {
        dnnm2 = x[1] - x[0];
        szdvdf_idx_1 = (y[2] - y[1]) / dnnm2;
        dzzdx = (szdvdf_idx_1 - y[0]) / dnnm2;
        szdvdf_idx_1 = (y[y.size(0) - 1] - szdvdf_idx_1) / dnnm2;
        pp1_coefs[0] = (szdvdf_idx_1 - dzzdx) / dnnm2;
        pp1_coefs[1] = 2.0 * dzzdx - szdvdf_idx_1;
        pp1_coefs[2] = y[0];
        pp1_coefs[3] = y[1];
        szs_idx_1 = pp.coefs.size(1);
        pp.coefs.set_size(&sd_emlrtRTEI, &c_st, 1, pp.coefs.size(1));
        for (nxm1 = 0; nxm1 < szs_idx_1; nxm1++) {
          pp.coefs[nxm1] = pp1_coefs[nxm1];
        }
      } else {
        pp.coefs[0] = (y[1] - y[0]) / (x[1] - x[0]);
        pp.coefs[1] = y[0];
      }
      pp.breaks.set_size(&rd_emlrtRTEI, &c_st, 1, 2);
      for (nxm1 = 0; nxm1 < 2; nxm1++) {
        pp.breaks[nxm1] = x[nxm1];
      }
    } else {
      szdvdf_idx_1 = x[1] - x[0];
      dnnm2 = (y[1] - y[0]) / szdvdf_idx_1;
      pp.coefs[0] = ((y[2] - y[1]) / (x[2] - x[1]) - dnnm2) / (x[2] - x[0]);
      pp.coefs[1] = dnnm2 - pp.coefs[0] * szdvdf_idx_1;
      pp.coefs[2] = y[0];
      pp.breaks.set_size(&rd_emlrtRTEI, &c_st, 1, 2);
      pp.breaks[0] = x[0];
      pp.breaks[1] = x[2];
    }
  } else {
    nxm1 = x.size(1) - 1;
    if (has_endslopes) {
      szdvdf_idx_1 = static_cast<real_T>(y.size(0)) - 3.0;
      szs_idx_1 = y.size(0) - 2;
      yoffset = 1;
    } else {
      szdvdf_idx_1 = static_cast<real_T>(y.size(0)) - 1.0;
      szs_idx_1 = y.size(0);
      yoffset = 0;
    }
    s.set_size(&kd_emlrtRTEI, &b_st, 1, s.size(1));
    if (szs_idx_1 < 0) {
      emlrtNonNegativeCheckR2012b(static_cast<real_T>(szs_idx_1), &e_emlrtDCI,
                                  &b_st);
    }
    s.set_size(&kd_emlrtRTEI, &b_st, s.size(0), szs_idx_1);
    dvdf.set_size(&ld_emlrtRTEI, &b_st, 1, dvdf.size(1));
    if (!(szdvdf_idx_1 >= 0.0)) {
      emlrtNonNegativeCheckR2012b(szdvdf_idx_1, &f_emlrtDCI, &b_st);
    }
    dvdf.set_size(&ld_emlrtRTEI, &b_st, dvdf.size(0),
                  static_cast<int32_T>(szdvdf_idx_1));
    dx.set_size(&md_emlrtRTEI, &b_st, 1, x.size(1) - 1);
    for (k = 0; k < nxm1; k++) {
      szdvdf_idx_1 = x[k + 1] - x[k];
      dx[k] = szdvdf_idx_1;
      szs_idx_1 = yoffset + k;
      dvdf[k] = (y[szs_idx_1 + 1] - y[szs_idx_1]) / szdvdf_idx_1;
    }
    for (k = 2; k <= nxm1; k++) {
      s[k - 1] = 3.0 * (dx[k - 1] * dvdf[k - 2] + dx[k - 2] * dvdf[k - 1]);
    }
    if (has_endslopes) {
      szdvdf_idx_1 = 0.0;
      dnnm2 = 0.0;
      s[0] = y[0] * dx[1];
      s[x.size(1) - 1] = dx[x.size(1) - 3] * y[x.size(1) + 1];
    } else {
      szdvdf_idx_1 = x[2] - x[0];
      dnnm2 = x[x.size(1) - 1] - x[x.size(1) - 3];
      s[0] = ((dx[0] + 2.0 * szdvdf_idx_1) * dx[1] * dvdf[0] +
              dx[0] * dx[0] * dvdf[1]) /
             szdvdf_idx_1;
      s[x.size(1) - 1] =
          ((dx[x.size(1) - 2] + 2.0 * dnnm2) * dx[x.size(1) - 3] *
               dvdf[x.size(1) - 2] +
           dx[x.size(1) - 2] * dx[x.size(1) - 2] * dvdf[x.size(1) - 3]) /
          dnnm2;
    }
    md.set_size(&nd_emlrtRTEI, &b_st, 1, x.size(1));
    md[0] = dx[1];
    md[x.size(1) - 1] = dx[x.size(1) - 3];
    for (k = 2; k <= nxm1; k++) {
      md[k - 1] = 2.0 * (dx[k - 1] + dx[k - 2]);
    }
    r = dx[1] / md[0];
    md[1] = md[1] - r * szdvdf_idx_1;
    s[1] = s[1] - r * s[0];
    for (k = 3; k <= nxm1; k++) {
      r = dx[k - 1] / md[k - 2];
      md[k - 1] = md[k - 1] - r * dx[k - 3];
      s[k - 1] = s[k - 1] - r * s[k - 2];
    }
    r = dnnm2 / md[x.size(1) - 2];
    md[x.size(1) - 1] = md[x.size(1) - 1] - r * dx[x.size(1) - 3];
    s[x.size(1) - 1] = s[x.size(1) - 1] - r * s[x.size(1) - 2];
    s[x.size(1) - 1] = s[x.size(1) - 1] / md[x.size(1) - 1];
    for (k = nxm1; k >= 2; k--) {
      s[k - 1] = (s[k - 1] - dx[k - 2] * s[k]) / md[k - 1];
    }
    s[0] = (s[0] - szdvdf_idx_1 * s[1]) / md[0];
    c_st.site = &ub_emlrtRSI;
    nxm1 = x.size(1);
    szs_idx_1 = s.size(1) - 1;
    if (s.size(1) - 1 < 0) {
      emlrtNonNegativeCheckR2012b(-1.0, &g_emlrtDCI, &c_st);
    }
    pp.coefs.set_size(&od_emlrtRTEI, &c_st, s.size(1) - 1, 4);
    for (k = 0; k <= nxm1 - 2; k++) {
      szdvdf_idx_1 = dvdf[k];
      dnnm2 = s[k];
      r = dx[k];
      dzzdx = (szdvdf_idx_1 - dnnm2) / r;
      szdvdf_idx_1 = (s[k + 1] - szdvdf_idx_1) / r;
      pp.coefs[k] = (szdvdf_idx_1 - dzzdx) / r;
      pp.coefs[szs_idx_1 + k] = 2.0 * dzzdx - szdvdf_idx_1;
      pp.coefs[(szs_idx_1 << 1) + k] = dnnm2;
      pp.coefs[3 * szs_idx_1 + k] = y[yoffset + k];
    }
    pp.breaks.set_size(&pd_emlrtRTEI, &b_st, 1, x.size(1));
    szs_idx_1 = x.size(1);
    for (nxm1 = 0; nxm1 < szs_idx_1; nxm1++) {
      pp.breaks[nxm1] = x[nxm1];
    }
  }
  st.site = &lb_emlrtRSI;
  if (xi.size(0) > 2147483646) {
    b_st.site = &y_emlrtRSI;
    check_forloop_overflow_error(&b_st);
  }
  szs_idx_1 = xi.size(0) - 1;
  emlrtEnterParallelRegion((emlrtCTX)sp,
                           static_cast<boolean_T>(omp_in_parallel()));
#pragma omp parallel num_threads(                                              \
    emlrtAllocRegionTLSs(sp->tls, omp_in_parallel(), omp_get_max_threads(),    \
                         omp_get_num_procs())) private(e_st)                   \
    firstprivate(d_st, emlrtHadParallelError)
  {
    try {

      d_st.prev = sp;
      d_st.tls = emlrtAllocTLS((emlrtCTX)sp, omp_get_thread_num());
      d_st.site = nullptr;
      e_st.prev = &d_st;
      e_st.tls = d_st.tls;
    } catch (...) {
      emlrtHadParallelError = true;
    }
#pragma omp for nowait
    for (b_k = 0; b_k <= szs_idx_1; b_k++) {
      if (emlrtHadParallelError)
        continue;
      try {

        if (muDoubleScalarIsNaN(xi[b_k])) {
          yi[b_k] = rtNaN;
        } else {
          e_st.site = &mb_emlrtRSI;
          yi[b_k] = ppval(&e_st, pp.breaks, pp.coefs, xi[b_k]);
        }
      } catch (...) {
        emlrtHadParallelError = true;
      }
    }
  }
  emlrtExitParallelRegion((emlrtCTX)sp,
                          static_cast<boolean_T>(omp_in_parallel()));
  emlrtHeapReferenceStackLeaveFcnR2012b((emlrtCTX)sp);
}

static void interp1SplineOrPCHIP(const emlrtStack *sp,
                                 const ::coder::array<real_T, 1U> &y,
                                 const ::coder::array<real_T, 2U> &xi,
                                 ::coder::array<real_T, 2U> &yi,
                                 const ::coder::array<real_T, 1U> &x)
{
  array<real_T, 2U> dvdf;
  array<real_T, 2U> s;
  array<real_T, 1U> dx;
  array<real_T, 1U> md;
  c_struct_T pp;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack d_st;
  emlrtStack e_st;
  emlrtStack st;
  real_T pp1_coefs[4];
  real_T d;
  real_T dnnm2;
  real_T r;
  real_T szdvdf_idx_1;
  int32_T b_pp[2];
  int32_T b_k;
  int32_T k;
  int32_T nxm1;
  int32_T szs_idx_1;
  int32_T yoffset;
  boolean_T emlrtHadParallelError{false};
  boolean_T exitg1;
  boolean_T has_endslopes;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b((emlrtCTX)sp);
  st.site = &kb_emlrtRSI;
  b_st.site = &nb_emlrtRSI;
  c_st.site = &ob_emlrtRSI;
  if (x.size(0) < 2) {
    emlrtErrorWithMessageIdR2018a(&c_st, &jb_emlrtRTEI,
                                  "MATLAB:chckxy:NotEnoughPts",
                                  "MATLAB:chckxy:NotEnoughPts", 0);
  }
  has_endslopes =
      ((!muDoubleScalarIsInf(x[0])) && (!muDoubleScalarIsNaN(x[0])));
  if (has_endslopes) {
    k = 0;
    exitg1 = false;
    while ((!exitg1) && (k <= x.size(0) - 2)) {
      if (!(x[k] < x[k + 1])) {
        has_endslopes = false;
        exitg1 = true;
      } else {
        k++;
      }
    }
  }
  if ((!has_endslopes) || muDoubleScalarIsInf(x[x.size(0) - 1])) {
    has_endslopes = false;
  }
  if (!has_endslopes) {
    emlrtErrorWithMessageIdR2018a(
        &c_st, &kb_emlrtRTEI, "Coder:toolbox:MustBeFiniteAndStrictlyIncreasing",
        "Coder:toolbox:MustBeFiniteAndStrictlyIncreasing", 0);
  }
  has_endslopes = false;
  k = 0;
  exitg1 = false;
  while ((!exitg1) && (k <= y.size(0) - 1)) {
    if (muDoubleScalarIsNaN(y[k])) {
      has_endslopes = true;
      exitg1 = true;
    } else {
      k++;
    }
  }
  if (has_endslopes) {
    emlrtErrorWithMessageIdR2018a(&c_st, &lb_emlrtRTEI,
                                  "Coder:toolbox:UnsupportedNaN",
                                  "Coder:toolbox:UnsupportedNaN", 0);
  }
  if ((y.size(0) != x.size(0)) && (y.size(0) != x.size(0) + 2)) {
    emlrtErrorWithMessageIdR2018a(&c_st, &mb_emlrtRTEI,
                                  "MATLAB:chckxy:NumSitesMismatchValues",
                                  "MATLAB:chckxy:NumSitesMismatchValues", 4, 12,
                                  x.size(0), 12, y.size(0));
  }
  has_endslopes = (y.size(0) == x.size(0) + 2);
  if ((x.size(0) <= 2) || ((x.size(0) <= 3) && (!has_endslopes))) {
    c_st.site = &pb_emlrtRSI;
    has_endslopes = (y.size(0) == x.size(0) + 2);
    if (x.size(0) <= 2) {
      if (has_endslopes) {
        yoffset = 4;
      } else {
        yoffset = 2;
      }
    } else {
      yoffset = 3;
    }
    pp.coefs.set_size(&qd_emlrtRTEI, &c_st, 1, yoffset);
    if (x.size(0) <= 2) {
      if (has_endslopes) {
        dnnm2 = x[1] - x[0];
        szdvdf_idx_1 = (y[2] - y[1]) / dnnm2;
        r = (szdvdf_idx_1 - y[0]) / dnnm2;
        szdvdf_idx_1 = (y[y.size(0) - 1] - szdvdf_idx_1) / dnnm2;
        pp1_coefs[0] = (szdvdf_idx_1 - r) / dnnm2;
        pp1_coefs[1] = 2.0 * r - szdvdf_idx_1;
        pp1_coefs[2] = y[0];
        pp1_coefs[3] = y[1];
        b_pp[1] = pp.coefs.size(1);
        pp.coefs.set_size(&sd_emlrtRTEI, &c_st, 1, pp.coefs.size(1));
        szs_idx_1 = b_pp[1];
        for (nxm1 = 0; nxm1 < szs_idx_1; nxm1++) {
          pp.coefs[nxm1] = pp1_coefs[nxm1];
        }
      } else {
        pp.coefs[0] = (y[1] - y[0]) / (x[1] - x[0]);
        pp.coefs[1] = y[0];
      }
      pp.breaks.set_size(&rd_emlrtRTEI, &c_st, 1, 2);
      for (nxm1 = 0; nxm1 < 2; nxm1++) {
        pp.breaks[nxm1] = x[nxm1];
      }
    } else {
      szdvdf_idx_1 = x[1] - x[0];
      dnnm2 = (y[1] - y[0]) / szdvdf_idx_1;
      pp.coefs[0] = ((y[2] - y[1]) / (x[2] - x[1]) - dnnm2) / (x[2] - x[0]);
      pp.coefs[1] = dnnm2 - pp.coefs[0] * szdvdf_idx_1;
      pp.coefs[2] = y[0];
      pp.breaks.set_size(&rd_emlrtRTEI, &c_st, 1, 2);
      pp.breaks[0] = x[0];
      pp.breaks[1] = x[2];
    }
  } else {
    nxm1 = x.size(0) - 1;
    if (has_endslopes) {
      szdvdf_idx_1 = static_cast<real_T>(y.size(0)) - 3.0;
      szs_idx_1 = y.size(0) - 2;
      yoffset = 1;
    } else {
      szdvdf_idx_1 = static_cast<real_T>(y.size(0)) - 1.0;
      szs_idx_1 = y.size(0);
      yoffset = 0;
    }
    s.set_size(&kd_emlrtRTEI, &b_st, 1, s.size(1));
    if (szs_idx_1 < 0) {
      emlrtNonNegativeCheckR2012b(static_cast<real_T>(szs_idx_1), &e_emlrtDCI,
                                  &b_st);
    }
    s.set_size(&kd_emlrtRTEI, &b_st, s.size(0), szs_idx_1);
    dvdf.set_size(&ld_emlrtRTEI, &b_st, 1, dvdf.size(1));
    if (!(szdvdf_idx_1 >= 0.0)) {
      emlrtNonNegativeCheckR2012b(szdvdf_idx_1, &f_emlrtDCI, &b_st);
    }
    dvdf.set_size(&ld_emlrtRTEI, &b_st, dvdf.size(0),
                  static_cast<int32_T>(szdvdf_idx_1));
    b_pp[0] = 1;
    b_pp[1] = x.size(0) - 1;
    c_st.site = &qb_emlrtRSI;
    internal::indexShapeCheck(&c_st, x.size(0), b_pp);
    dx.set_size(&md_emlrtRTEI, &b_st, x.size(0) - 1);
    for (k = 0; k < nxm1; k++) {
      szdvdf_idx_1 = x[k + 1] - x[k];
      dx[k] = szdvdf_idx_1;
      szs_idx_1 = yoffset + k;
      dvdf[k] = (y[szs_idx_1 + 1] - y[szs_idx_1]) / szdvdf_idx_1;
    }
    for (k = 2; k <= nxm1; k++) {
      s[k - 1] = 3.0 * (dx[k - 1] * dvdf[k - 2] + dx[k - 2] * dvdf[k - 1]);
    }
    if (has_endslopes) {
      szdvdf_idx_1 = 0.0;
      dnnm2 = 0.0;
      s[0] = y[0] * dx[1];
      s[x.size(0) - 1] = dx[x.size(0) - 3] * y[x.size(0) + 1];
    } else {
      szdvdf_idx_1 = x[2] - x[0];
      dnnm2 = x[x.size(0) - 1] - x[x.size(0) - 3];
      s[0] = ((dx[0] + 2.0 * szdvdf_idx_1) * dx[1] * dvdf[0] +
              dx[0] * dx[0] * dvdf[1]) /
             szdvdf_idx_1;
      s[x.size(0) - 1] =
          ((dx[x.size(0) - 2] + 2.0 * dnnm2) * dx[x.size(0) - 3] *
               dvdf[x.size(0) - 2] +
           dx[x.size(0) - 2] * dx[x.size(0) - 2] * dvdf[x.size(0) - 3]) /
          dnnm2;
    }
    md.set_size(&nd_emlrtRTEI, &b_st, x.size(0));
    md[0] = dx[1];
    md[x.size(0) - 1] = dx[x.size(0) - 3];
    for (k = 2; k <= nxm1; k++) {
      md[k - 1] = 2.0 * (dx[k - 1] + dx[k - 2]);
    }
    r = dx[1] / md[0];
    md[1] = md[1] - r * szdvdf_idx_1;
    s[1] = s[1] - r * s[0];
    for (k = 3; k <= nxm1; k++) {
      r = dx[k - 1] / md[k - 2];
      md[k - 1] = md[k - 1] - r * dx[k - 3];
      s[k - 1] = s[k - 1] - r * s[k - 2];
    }
    r = dnnm2 / md[x.size(0) - 2];
    md[x.size(0) - 1] = md[x.size(0) - 1] - r * dx[x.size(0) - 3];
    s[x.size(0) - 1] = s[x.size(0) - 1] - r * s[x.size(0) - 2];
    s[x.size(0) - 1] = s[x.size(0) - 1] / md[x.size(0) - 1];
    for (k = nxm1; k >= 2; k--) {
      s[k - 1] = (s[k - 1] - dx[k - 2] * s[k]) / md[k - 1];
    }
    s[0] = (s[0] - szdvdf_idx_1 * s[1]) / md[0];
    c_st.site = &ub_emlrtRSI;
    nxm1 = x.size(0);
    szs_idx_1 = s.size(1) - 1;
    if (s.size(1) - 1 < 0) {
      emlrtNonNegativeCheckR2012b(-1.0, &g_emlrtDCI, &c_st);
    }
    pp.coefs.set_size(&od_emlrtRTEI, &c_st, s.size(1) - 1, 4);
    for (k = 0; k <= nxm1 - 2; k++) {
      szdvdf_idx_1 = dvdf[k];
      dnnm2 = s[k];
      r = (szdvdf_idx_1 - dnnm2) / dx[k];
      szdvdf_idx_1 = (s[k + 1] - szdvdf_idx_1) / dx[k];
      pp.coefs[k] = (szdvdf_idx_1 - r) / dx[k];
      pp.coefs[szs_idx_1 + k] = 2.0 * r - szdvdf_idx_1;
      pp.coefs[(szs_idx_1 << 1) + k] = dnnm2;
      pp.coefs[3 * szs_idx_1 + k] = y[yoffset + k];
    }
    pp.breaks.set_size(&pd_emlrtRTEI, &b_st, 1, x.size(0));
    szs_idx_1 = x.size(0);
    for (nxm1 = 0; nxm1 < szs_idx_1; nxm1++) {
      pp.breaks[nxm1] = x[nxm1];
    }
  }
  st.site = &lb_emlrtRSI;
  if (xi.size(1) > 2147483646) {
    b_st.site = &y_emlrtRSI;
    check_forloop_overflow_error(&b_st);
  }
  szs_idx_1 = xi.size(1) - 1;
  emlrtEnterParallelRegion((emlrtCTX)sp,
                           static_cast<boolean_T>(omp_in_parallel()));
#pragma omp parallel num_threads(                                              \
    emlrtAllocRegionTLSs(sp->tls, omp_in_parallel(), omp_get_max_threads(),    \
                         omp_get_num_procs())) private(e_st, d)                \
    firstprivate(d_st, emlrtHadParallelError)
  {
    try {

      d_st.prev = sp;
      d_st.tls = emlrtAllocTLS((emlrtCTX)sp, omp_get_thread_num());
      d_st.site = nullptr;
      e_st.prev = &d_st;
      e_st.tls = d_st.tls;
    } catch (...) {
      emlrtHadParallelError = true;
    }
#pragma omp for nowait
    for (b_k = 0; b_k <= szs_idx_1; b_k++) {
      if (emlrtHadParallelError)
        continue;
      try {

        d = xi[b_k];
        if (muDoubleScalarIsNaN(d)) {
          yi[b_k] = rtNaN;
        } else {
          e_st.site = &mb_emlrtRSI;
          yi[b_k] = ppval(&e_st, pp.breaks, pp.coefs, d);
        }
      } catch (...) {
        emlrtHadParallelError = true;
      }
    }
  }
  emlrtExitParallelRegion((emlrtCTX)sp,
                          static_cast<boolean_T>(omp_in_parallel()));
  emlrtHeapReferenceStackLeaveFcnR2012b((emlrtCTX)sp);
}

void interp1(const emlrtStack *sp, const ::coder::array<real_T, 2U> &varargin_1,
             const ::coder::array<real_T, 1U> &varargin_2,
             const ::coder::array<real_T, 1U> &varargin_3,
             ::coder::array<real_T, 1U> &Vq)
{
  array<real_T, 2U> x;
  array<real_T, 1U> y;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack st;
  int32_T k;
  int32_T n;
  int32_T nx;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b((emlrtCTX)sp);
  st.site = &db_emlrtRSI;
  y.set_size(&cc_emlrtRTEI, &st, varargin_2.size(0));
  n = varargin_2.size(0);
  for (k = 0; k < n; k++) {
    y[k] = varargin_2[k];
  }
  x.set_size(&dc_emlrtRTEI, &st, 1, varargin_1.size(1));
  n = varargin_1.size(1);
  for (k = 0; k < n; k++) {
    x[k] = varargin_1[k];
  }
  nx = varargin_1.size(1);
  if (varargin_2.size(0) != varargin_1.size(1)) {
    emlrtErrorWithMessageIdR2018a(&st, &e_emlrtRTEI,
                                  "Coder:MATLAB:interp1_YInvalidNumRows",
                                  "Coder:MATLAB:interp1_YInvalidNumRows", 0);
  }
  if (varargin_1.size(1) <= 1) {
    emlrtErrorWithMessageIdR2018a(&st, &f_emlrtRTEI,
                                  "MATLAB:interp1:NotEnoughPts",
                                  "MATLAB:interp1:NotEnoughPts", 0);
  }
  Vq.set_size(&ec_emlrtRTEI, &st, varargin_3.size(0));
  n = varargin_3.size(0);
  for (k = 0; k < n; k++) {
    Vq[k] = 0.0;
  }
  if (varargin_3.size(0) != 0) {
    b_st.site = &eb_emlrtRSI;
    if (nx > 2147483646) {
      c_st.site = &y_emlrtRSI;
      check_forloop_overflow_error(&c_st);
    }
    k = 0;
    int32_T exitg1;
    do {
      exitg1 = 0;
      if (k <= nx - 1) {
        if (muDoubleScalarIsNaN(varargin_1[k])) {
          emlrtErrorWithMessageIdR2018a(&st, &g_emlrtRTEI,
                                        "MATLAB:interp1:NaNinX",
                                        "MATLAB:interp1:NaNinX", 0);
        } else {
          k++;
        }
      } else {
        if (varargin_1[1] < varargin_1[0]) {
          real_T xtmp;
          int32_T nd2;
          k = nx >> 1;
          b_st.site = &fb_emlrtRSI;
          for (nd2 = 0; nd2 < k; nd2++) {
            xtmp = x[nd2];
            n = (nx - nd2) - 1;
            x[nd2] = x[n];
            x[n] = xtmp;
          }
          b_st.site = &gb_emlrtRSI;
          if (y.size(0) > 1) {
            n = y.size(0) - 1;
            nd2 = y.size(0) >> 1;
            for (k = 0; k < nd2; k++) {
              xtmp = y[k];
              y[k] = y[n - k];
              y[n - k] = xtmp;
            }
          }
        }
        b_st.site = &hb_emlrtRSI;
        for (k = 2; k <= nx; k++) {
          if (x[k - 1] <= x[k - 2]) {
            emlrtErrorWithMessageIdR2018a(
                &st, &h_emlrtRTEI, "Coder:toolbox:interp1_nonMonotonicX",
                "Coder:toolbox:interp1_nonMonotonicX", 0);
          }
        }
        b_st.site = &ib_emlrtRSI;
        interp1SplineOrPCHIP(&b_st, y, varargin_3, Vq, x);
        exitg1 = 1;
      }
    } while (exitg1 == 0);
  }
  emlrtHeapReferenceStackLeaveFcnR2012b((emlrtCTX)sp);
}

void interp1(const emlrtStack *sp, const ::coder::array<real_T, 1U> &varargin_1,
             const ::coder::array<real_T, 1U> &varargin_2,
             const ::coder::array<real_T, 2U> &varargin_3,
             ::coder::array<real_T, 2U> &Vq)
{
  array<real_T, 1U> x;
  array<real_T, 1U> y;
  emlrtStack b_st;
  emlrtStack c_st;
  emlrtStack st;
  int32_T k;
  int32_T n;
  int32_T nx;
  boolean_T b;
  st.prev = sp;
  st.tls = sp->tls;
  b_st.prev = &st;
  b_st.tls = st.tls;
  c_st.prev = &b_st;
  c_st.tls = b_st.tls;
  emlrtHeapReferenceStackEnterFcnR2012b((emlrtCTX)sp);
  st.site = &db_emlrtRSI;
  y.set_size(&cc_emlrtRTEI, &st, varargin_2.size(0));
  n = varargin_2.size(0);
  for (k = 0; k < n; k++) {
    y[k] = varargin_2[k];
  }
  x.set_size(&dc_emlrtRTEI, &st, varargin_1.size(0));
  n = varargin_1.size(0);
  for (k = 0; k < n; k++) {
    x[k] = varargin_1[k];
  }
  nx = varargin_1.size(0);
  if (varargin_1.size(0) != varargin_2.size(0)) {
    emlrtErrorWithMessageIdR2018a(&st, &e_emlrtRTEI,
                                  "Coder:MATLAB:interp1_YInvalidNumRows",
                                  "Coder:MATLAB:interp1_YInvalidNumRows", 0);
  }
  if (varargin_1.size(0) <= 1) {
    emlrtErrorWithMessageIdR2018a(&st, &f_emlrtRTEI,
                                  "MATLAB:interp1:NotEnoughPts",
                                  "MATLAB:interp1:NotEnoughPts", 0);
  }
  Vq.set_size(&ec_emlrtRTEI, &st, 1, varargin_3.size(1));
  n = varargin_3.size(1);
  for (k = 0; k < n; k++) {
    Vq[k] = 0.0;
  }
  b = (varargin_3.size(1) == 0);
  if (!b) {
    b_st.site = &eb_emlrtRSI;
    if (nx > 2147483646) {
      c_st.site = &y_emlrtRSI;
      check_forloop_overflow_error(&c_st);
    }
    k = 0;
    int32_T exitg1;
    do {
      exitg1 = 0;
      if (k <= nx - 1) {
        if (muDoubleScalarIsNaN(varargin_1[k])) {
          emlrtErrorWithMessageIdR2018a(&st, &g_emlrtRTEI,
                                        "MATLAB:interp1:NaNinX",
                                        "MATLAB:interp1:NaNinX", 0);
        } else {
          k++;
        }
      } else {
        if (varargin_1[1] < varargin_1[0]) {
          real_T xtmp;
          int32_T nd2;
          k = nx >> 1;
          b_st.site = &fb_emlrtRSI;
          for (nd2 = 0; nd2 < k; nd2++) {
            xtmp = x[nd2];
            n = (nx - nd2) - 1;
            x[nd2] = x[n];
            x[n] = xtmp;
          }
          b_st.site = &gb_emlrtRSI;
          if (y.size(0) > 1) {
            n = y.size(0) - 1;
            nd2 = y.size(0) >> 1;
            for (k = 0; k < nd2; k++) {
              xtmp = y[k];
              y[k] = y[n - k];
              y[n - k] = xtmp;
            }
          }
        }
        b_st.site = &hb_emlrtRSI;
        for (k = 2; k <= nx; k++) {
          if (x[k - 1] <= x[k - 2]) {
            emlrtErrorWithMessageIdR2018a(
                &st, &h_emlrtRTEI, "Coder:toolbox:interp1_nonMonotonicX",
                "Coder:toolbox:interp1_nonMonotonicX", 0);
          }
        }
        b_st.site = &ib_emlrtRSI;
        interp1SplineOrPCHIP(&b_st, y, varargin_3, Vq, x);
        exitg1 = 1;
      }
    } while (exitg1 == 0);
  }
  emlrtHeapReferenceStackLeaveFcnR2012b((emlrtCTX)sp);
}

} // namespace coder

// End of code generation (interp1.cpp)
